package com.hvtt.web.connection;

import com.hvtt.web.utilities.Utilities;
import java.sql.Connection;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;

public class DatasourceConnection {

    private static DataSource datasource = null;

    DatasourceConnection() {
    }

    public static Connection getConnection() {
        try {
            if (datasource == null) {
                DatasourceConnection.initDatasource();
            }
            return datasource.getConnection();
        } catch (Exception e) {
            return null;
        }
    }

    public static void initDatasource() {

        PoolProperties p = new PoolProperties();

        p.setUrl("jdbc:mysql://" + Utilities.getPropertiesKey("com.hvtt.param.connection.server")+":" + Utilities.getPropertiesKey("com.hvtt.param.connection.port") +  "/" + Utilities.getPropertiesKey("com.hvtt.param.connection.database") + "?noAccessToProcedureBodies=true&characterEncoding=UTF-8");
        p.setDriverClassName("com.mysql.jdbc.Driver");
        p.setUsername(Utilities.getPropertiesKey("com.hvtt.param.smtp.username"));
        p.setPassword(Utilities.getPropertiesKey("com.hvtt.param.smtp.password"));
        p.setJmxEnabled(true);
        p.setTestWhileIdle(false);
        p.setTestOnBorrow(true);
        p.setValidationQuery("SELECT 1");
        p.setTestOnReturn(false);
        p.setValidationInterval(30000);
        p.setTimeBetweenEvictionRunsMillis(30000);
        p.setMaxActive(100);
        p.setInitialSize(5);
        p.setMaxWait(10000);
        p.setRemoveAbandonedTimeout(60);
        p.setMinEvictableIdleTimeMillis(30000);
        p.setMinIdle(5);
        p.setLogAbandoned(true);
        p.setRemoveAbandoned(true);
        p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
                + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
        datasource = new DataSource();
        datasource.setPoolProperties(p);

    }

    public static void closeDatasource() {
        datasource.close();
    }
}